<?php 
/**
 * Класс, который реализовует Extjs Grid
 *
 */

class FRM_View_Ext_Galery extends FRM_View_Ext_Abstract
{
	/**
	 * Enter description here...
	 *
	 * @var int
	 */
	protected $parentField = "id_parent";
	
	/**
	 * Enter description here...
	 *
	 * @var PhpExt_XTemplate
	 */
	protected $tpl;
	
	/**
	 * Enter description here...
	 *
	 * @var string
	 */
	protected $renderTo;

	/**
	 * Enter description here...
	 *
	 * @param string $parentField
	 */
	public function setParentField ($parentField){
		$this->parentField = $parentField;
	}
	/**
	 * Enter description here...
	 *
	 * @param string $tpl
	 */
	public function setTpl($tpl){
		$this->tpl = $tpl;
	}	
	
	/**
	 * Enter description here...
	 *
	 * @param string $renderTo
	 */
	public function setRenderTo($renderTo){
		$this->renderTo = $renderTo;
	}	

	/**
	 * Enter description here...
	 *
	 * @var int
	 */
	protected $baseParams = array();

	public function getJavaScript(){

		$this->preRender();

		$width=	(isset($this->width))?$this->width:null;
		$height=	(isset($this->height))?$this->height:null;
		$id=(isset($this->id))?$this->id:null;
		$renderTo=(isset($this->renderTo))?$this->renderTo:null;
		$url=(isset($this->url))?$this->url:null;
		$id_tf=(isset($field['id_tf']))?$field['id_tf']:null;
		$tplGalery=(isset($this->tpl))?$this->tpl:null;


		$reader = new PhpExt_Data_JsonReader();
		$reader		-> setRoot('images');
		$reader		-> setId('id');
		$reader		-> addField(new PhpExt_Data_FieldConfigObject('id','id'));
		$reader		-> addField(new PhpExt_Data_FieldConfigObject('photo','photo'));
		$reader		-> addField(new PhpExt_Data_FieldConfigObject('is_main','is_main'));

		$http = new PhpExt_Data_HttpProxy();
		$http -> setUrl($url);

		$imagesStore = new PhpExt_Data_Store();
		$imagesStore -> setProxy($http)
		-> setBaseParams(array('id_tf'=>$id_tf))
		-> setReader($reader);

		$dataViev = new PhpExt_DataView("div.thumb-wrap");
		$dataViev ->setStore($imagesStore)
					->setTemplate($tplGalery)
					->setSimpleSelect(true)
					->setLoadingText("Загрузка...")
					->setSingleSelect(true)
					->setSelectedCssClass("x-view-selected")
					->setOverCssClass("x-view-over")
					->setEmptyText("Изображения отсутствуют");
					
		//Top Toolbar
		$removeHandler = new PhpExt_Handler();
		$removeHandler = PhpExt_Javascript::inlineStm("function() {
	      var m = Ext.getCmp('galery').items.get(0).getSelectedRecords();
	      if(m.length > 0) {
	         Ext.MessageBox.confirm('Информация', 'Вы действительно хотите удалить выбранный элемент?', function(btn) {
	            if(btn == 'yes') {
	                m[0].store.remove(m[0]);
	                var strC = new Ext.data.Store( {
	                  autoLoad : false, baseParams : {'id_offer':m[0].store.baseParams.id}
	                  , failure : function(form, action) {
	                     Ext.MessageBox.alert('Ошибка', action.result.msg); }, success: function (){ m[0].store.load();}
	                  , proxy : new Ext.data.HttpProxy( {
	                     url : '/".self::$module."/gallery/delete/'+m[0].get('id')+'/'}
	                  ), remoteSort : false}
	               ); strC.load({callback:function (){ m[0].store.load();}});}
	            }
	         );
	         }
	      else {
	         Ext.MessageBox.alert('Информация', 'Нет выбраных элементов');
	         }
	      }");
		$upHandler = new PhpExt_Handler();
		$upHandler = PhpExt_Javascript::inlineStm("function() {
	      var m = Ext.getCmp('galery').items.get(0).getSelectedRecords();
	      if(m.length > 0) {
	               var strC = new Ext.data.Store( {
	                  autoLoad : false, baseParams : {'id_offer':m[0].store.baseParams.id}
	                  , failure : function(form, action) {
	                     Ext.MessageBox.alert('Ошибка', action.result.msg); }, success: function (){ m[0].store.load();}
	                  , proxy : new Ext.data.HttpProxy( {
	                     url : '/".self::$module."/gallery/up/'+m[0].get('id')+'/'}
	                  ), remoteSort : false}
	               ); strC.load({callback:function (){ m[0].store.load();}});
	         }
	      else {
	         Ext.MessageBox.alert('Информация', 'Нет выбраных элементов');
	         }
	      }");		
		$downHandler = new PhpExt_Handler();
		$downHandler = PhpExt_Javascript::inlineStm("function() {
	      var m = Ext.getCmp('galery').items.get(0).getSelectedRecords();
	      if(m.length > 0) {
	      
	               var strC = new Ext.data.Store( {
	                  autoLoad : false, baseParams : {'id_offer':m[0].store.baseParams.id}
	                  , failure : function(form, action) {
	                     Ext.MessageBox.alert('Ошибка', action.result.msg); }
	                  , proxy : new Ext.data.HttpProxy( {
	                     url : '/".self::$module."/gallery/down/'+m[0].get('id')+'/'}
	                  ), remoteSort : false}
	               ); strC.load({callback:function (){ m[0].store.load();}});
	         }
	      else {
	         Ext.MessageBox.alert('Информация', 'Нет выбраных элементов');
	         }
	      }");	
		$mainHandler = new PhpExt_Handler();
		$mainHandler = PhpExt_Javascript::inlineStm("function() {
	      var m = Ext.getCmp('galery').items.get(0).getSelectedRecords();
	      if(m.length > 0) {
	               var strC = new Ext.data.Store( {
	                  autoLoad : false, baseParams : {'id_offer':m[0].store.baseParams.id}
	                  , failure : function(form, action) {
	                     Ext.MessageBox.alert('Ошибка', action.result.msg); }, success: function (){ m[0].store.load();}
	                  , proxy : new Ext.data.HttpProxy( {
	                     url : '/".self::$module."/gallery/main/'+m[0].get('id')+'/'}
	                  ), remoteSort : false,listeners: {load: function (){ m[0].store.load();}}}
	               ); strC.load({callback:function (){ m[0].store.load();}});
	         }
	      else {
	         Ext.MessageBox.alert('Информация', 'Нет выбраных элементов');
	         }
	      }");	
		$tbar = new PhpExt_Toolbar_Toolbar();
		$tbar ->addButton("up","Вверх",TEMP_JS."/ext/resources/images/default/grid/up2.gif",$upHandler);
		$tbar ->addButton("down","Вниз",TEMP_JS."/ext/resources/images/default/grid/down2.gif",$downHandler);
		$tbar ->addButton("main","Сделать главным",TEMP_JS."/ext/resources/images/default/grid/main.gif",$mainHandler);
		$tbar ->addButton("remove","Удалить",TEMP_JS."/ext/resources/images/default/grid/remove.gif",$removeHandler);

		$item = new PhpExt_Panel();
		$item->setLayout(new PhpExt_Layout_FitLayout())
		->setTopToolbar($tbar)
		->setId($id)
		->setAutoScroll(true);
		$item->setAutoHeight(true);
		$item->setRenderTo($renderTo);
		$item->setHeight($height);
		$item->setWidth($width);
		$item->setCssStyle("padding-bottom:10px;");

		$item->addItem($dataViev);
		
		
		return $item->getJavascript(false);
	}
}